<?php
App::uses('AppController', 'Controller');

/**
 *
 */
class LoginController extends AppController {
	public $uses = array('User');
	public function beforeFilter() {
		parent::beforeFilter();
		//$this -> Auth -> allow();
	}

	/**
	 * index method
	 */
	public function admin_index() {
		$this -> layout = false;
		$this -> loadModel('User');
		//if already logged-in, redirect
		if ($this -> Session -> check('Auth.User')) {
			$user = $this->Session->read('Auth.User');
			// member can not access
			if($user['group_id'] !=3){
				$this -> redirect('/admin');
			}else{
				$this -> redirect('/Member/manage');
			}
		}

		// if we get the post information, try to authenticate
		if ($this -> request -> is('post')) {
			$groupId =  $this ->User->getGroupId($this -> request->data['User']['email']);
			if($groupId == -1){
				$this -> Session -> setFlash(__('Vui lòng kiểm tra lại email hoặc mật khẩu'));
				return;
			}
			if ($groupId == 3){
				$this -> Session -> setFlash(__('Không thể đăng nhập với email này'));
				return;
			}
			if ($this -> Auth -> login()) {
				$uploadURL = Router::url('/') . 'app/webroot/files/thongtin';
				$_SESSION['KCFINDER'] = array(
					'disabled' => false,
					'uploadURL' => $uploadURL,
					'uploadDir' => ''
				);
				$this -> Session -> write('Auth.User.updated_at', date('m-d-Y H:i:s'));
				$this -> redirect('/admin');
			} else {
				$this -> Session -> setFlash(__('Vui lòng kiểm tra lại email hoặc mật khẩu'));
			}
		}
	}

	public function admin_logout() {
		$_SESSION['KCEDITOR']['disabled'] = true;
		unset($_SESSION['KCEDITOR']);
		$uid = $this -> Session -> read('Auth.User.id');
		$this -> loadModel('User');
		$u = $this -> Session -> read('Auth.User');
		/*$u['updated_at'] = date("Y-m-d H:i:s");
		$this -> User -> save($u);*/
		$this -> User ->id = $uid;
		$this->User->saveField('updated_at', date("Y-m-d H:i:s"));
		$this -> Session -> setFlash("");
		return $this->redirect($this->Auth->logout());
	}
	

}
?>